package com.test.fundible.fundible_00_utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.test.fundible.R;


public class Loading_fundibleDialog extends Dialog {

    private ImageView gifIv;
    private TextView textView;

    public Loading_fundibleDialog(@NonNull Context context) {
        super(context);
    }

    private static boolean isShowing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fundible_loading);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        gifIv = findViewById(R.id.afl2_iv_gif);
        textView = findViewById(R.id.afl2_tv);

    }

    public void show(String text) {

        if (isShowing) return;

        super.show();

        isShowing = true;

        if (!TextUtils.isEmpty(text)) textView.setText(text);
        Glide.with(getContext()).asGif().load(R.drawable.gif_loading).into(gifIv);

        new Thread(() -> {
            try {
                Thread.sleep(10000);
                FundibleUtil.safeRun(this::dismiss);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    @Override
    public void dismiss() {
        if (getContext() == null) return;
        super.dismiss();
        isShowing = false;
    }
}
